package com.zz.admin.auth.controller;

import com.zz.admin.auth.dto.BaseDataDto;
import com.zz.admin.auth.dto.LoginBackDto;
import com.zz.admin.auth.dto.LoginOrRegisterDto;
import com.zz.admin.auth.service.AuthService;
import com.zz.common.base.dto.ResponseDTO;
import com.zz.common.base.dto.ResultDto;
import com.zz.common.constants.ApiConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@Api(tags = "认证授权接口")
@RestController
@RequestMapping("auth/v1.0")
public class AuthController {

    @Resource
    private AuthService authService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "认证授权-登录", notes = "registerDto")
    public ResponseDTO<LoginBackDto> login(@RequestBody LoginOrRegisterDto loginOrRegisterDto) {
        ResponseDTO<LoginBackDto> res = new ResponseDTO<>();
        res.setData(authService.login(loginOrRegisterDto));
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ApiOperation(value = "认证授权-登出", notes = "logout")
    public ResultDto logout() {
        ResultDto res = new ResultDto();
        authService.logout();
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/token/{tokenId}", method = RequestMethod.GET)
    @ApiOperation(value = "认证授权-获取token", notes = "token")
    public ResponseDTO<String> token(@PathVariable("tokenId") String tokenId, HttpSession session) {
        ResponseDTO<String> res = new ResponseDTO<>();
        res.setData(authService.getToken(tokenId, session));
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/token/refresh", method = RequestMethod.GET)
    @ApiOperation(value = "认证授权-刷新token", notes = "refresh")
    public ResponseDTO<String> refresh() {
        ResponseDTO<String> res = new ResponseDTO<>();
        res.setData(authService.refreshToken());
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/get-base-data/{userId}", method = RequestMethod.GET)
    @ApiOperation(value = "认证授权-获取基础数据", notes = "get-base-data")
    public ResponseDTO<BaseDataDto> getBaseData(@PathVariable("userId") String userId) {
        ResponseDTO<BaseDataDto> res = new ResponseDTO<>();
        res.setData(authService.getBaseData(userId));
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }


    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    @ApiOperation(value = "测试接口1", notes = "test1")
    public ResponseDTO<String> test1() {
        ResponseDTO<String> res = new ResponseDTO<>();
        String password = "12345";
        String encodedPassword = passwordEncoder.encode(password);
        System.out.println(encodedPassword);
        System.out.println(passwordEncoder.matches("12345", encodedPassword));
        res.setData(encodedPassword);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    @ApiOperation(value = "测试接口2", notes = "test2")
    @PreAuthorize("hasAnyRole('root','admin')")
    public ResponseDTO<String> test2() {
        ResponseDTO<String> res = new ResponseDTO<>();
        res.setData("test2");
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    @ApiOperation(value = "测试接口3", notes = "redirect")
    public ResponseDTO<String> redirect(@Parameter(description = "code") String code) {
        ResponseDTO<String> res = new ResponseDTO<>();
        res.setData(code);
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    @ApiOperation(value = "测试接口3", notes = "redirect")
    public ResponseDTO<String> home() {
        ResponseDTO<String> res = new ResponseDTO<>();
        res.setData("home");
        res.setCode(ApiConstants.RESULT_SUCCESS);
        res.setMessage(ApiConstants.MESSAGE_SUCCESS);
        return res;
    }

}
